package main.java.com.fronted.css;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

public class main_page extends JFrame{
    public static void create_page2(){

        JFrame frame1 = new JFrame("Main page");
        frame1.setLayout(null);
        frame1.setResizable(false);

        JPanel jp1 = new JPanel();
        jp1.setLayout(null);
        ImageIcon bg = new ImageIcon("src/main/resources/2.jpg");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(), bg.getIconHeight());
        frame1.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));

        //将图片load到panel面板中
        Font font = new Font("Arial", Font.BOLD, 16);

        JButton jb1 = new JButton("Meta-cosmic lab");//设置按钮
        jb1.setLayout(null);
        jb1.setFont(font);
        jb1.setBounds(150,300,200,30);
        jb1.setUI(new New_botton.RoundedButtonUI());
        frame1.add(jb1);
        B1Listener B1 = new B1Listener();
        jb1.addActionListener(B1);

        JButton jb2 = new JButton("flight control ");//设置按钮
        jb2.setLayout(null);
        jb2.setFont(font);
        jb2.setBounds(450,300,200,30);
        jb2.setUI(new New_botton.RoundedButtonUI());
        B2Listener B2 = new B2Listener();
        jb2.addActionListener(B2);
        frame1.add(jb2);

        //图片格式压缩到指定大小
        //只能使用一次，否则图片会反复压缩
        /*
        try{
            File inputFile = new File("src/main/resources/page1.png");
            BufferedImage inputImage = ImageIO.read(inputFile);
            int compressedWidth = 200;
            int compressedHeight = 200;
            BufferedImage outputImage = new BufferedImage(compressedWidth, compressedHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = outputImage.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2D.drawImage(inputImage, 0, 0, compressedWidth, compressedHeight, null);
            graphics2D.dispose();
            File outputFile = new File("src/main/resources/page1.png");
            ImageIO.write(outputImage, "png", outputFile);
        }catch(IOException e){
            e.printStackTrace();
        }
        try{
            File inputFile = new File("src/main/resources/page2.png");
            BufferedImage inputImage = ImageIO.read(inputFile);
            int compressedWidth = 200;
            int compressedHeight = 200;
            BufferedImage outputImage = new BufferedImage(compressedWidth, compressedHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = outputImage.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2D.drawImage(inputImage, 0, 0, compressedWidth, compressedHeight, null);
            graphics2D.dispose();
            File outputFile = new File("src/main/resources/page2.png");
            ImageIO.write(outputImage, "png", outputFile);
        }catch(IOException e){
            e.printStackTrace();
        }
        */

        //添加图片1
        //

        JLabel l2 = new JLabel();
        ImageIcon imageIcon1 = new ImageIcon("src/main/resources/page1.png");
        l2.setIcon(imageIcon1);
        l2.setBounds(150, 50, imageIcon1.getIconWidth(), imageIcon1.getIconHeight());
        frame1.add(l2);

        //设置图片的黑色边框
        try {
            BufferedImage originalImage = ImageIO.read(new File("src/main/resources/page1.png"));
            int borderWidth = 8; // 边框宽度
            int width = 200 + 2 * borderWidth;
            int height = 200 + 2 * borderWidth;
            BufferedImage borderedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = borderedImage.createGraphics();
            g2d.setColor(Color.white);
            g2d.fillRect(150, 50, width, height);
            g2d.drawImage(originalImage, borderWidth, borderWidth, null);
            // 释放资源
            JLabel imageLabel = new JLabel(new ImageIcon(borderedImage));
            // 将标签添加到窗口中
            frame1.getContentPane().add(imageLabel);
            g2d.dispose();
        }catch (Exception e) {
            e.printStackTrace();
        }

        //添加图片2
        JLabel l3 = new JLabel();
        ImageIcon imageIcon2 = new ImageIcon("src/main/resources/page2.png");
        l3.setIcon(imageIcon2);
        l3.setBounds(450, 50, imageIcon2.getIconWidth(), imageIcon2.getIconHeight());
        frame1.add(l3);

        frame1.setVisible(true);
        frame1.setSize(bg.getIconWidth(), bg.getIconHeight());
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static class B1Listener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            new Meta_cosmic().enter_meta_cosmic();
        }
    }

    public static class B2Listener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            new flight_control().frame_create();
        }
    }

    public static void main(String[] args) {
        create_page2();
    }
    //前端开发完毕，后端构思后端思路
    //关于函数调用下次再写，后端界面需要结合实验的具体内容进行编程
    //后端接口即为上述两个按钮，可以直接调用其他程序
}
